package com.evl.wxpay.util.paytype.verify;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.util.StringUtils;

import com.evl.api.qrcode.service.IWeiXinService;
import com.evl.api.qrcode.vo.TemplateRQ;
import com.evl.common.constant.OrderConst;
import com.evl.common.persistence.dao.RefundOrderMapper;
import com.evl.common.persistence.model.RefundOrder;
import com.evl.common.persistence.model.WxPayRecord;
import com.evl.core.vo.BaseResultVo;
import com.evl.core.vo.JsonReturnHelper;
import com.evl.dictionaryConfig.DictConfig;
import com.evl.dictionaryConfig.DictConst;
import com.evl.utils.BeanUtils;
import com.evl.utils.DateUtils;
import com.evl.wxpay.util.IWXPay;
import com.evl.wxpay.util.paytype.PayType;
import com.google.gson.JsonObject;

/**
 * 微信小程序支付，下单类型，退款
 * @author dangsheng
 *
 */

public class RefundVerify extends Verify{

	@Override
	public Map required(Object obj) throws Exception{
		RefundOrder refundOrder = (RefundOrder)obj;
		Map map = new HashMap();
		map.put("out_trade_no", refundOrder.getOrderNo());
		map.put("out_refund_no", refundOrder.getRefundNo());
//		map.put("total_fee", getPriceInt100(refundOrder.getRefundPrice()));
//		map.put("refund_fee", getPriceInt100(refundOrder.getRefundPrice()));
		map.put("total_fee", "1");
		map.put("refund_fee", "1");
		map.put("refund_fee_type", "CNY");
		map.put("op_user_id", refundOrder.getOpenid()); 
		return map;
	}
	
	@Override
	public WxPayRecord logRecord(WxPayRecord wxPayRecord,Object obj){
		RefundOrder refundOrder = (RefundOrder)obj;
		wxPayRecord.setTicketId(refundOrder.getTicketId());
		wxPayRecord.setOrderId(StringUtils.isEmpty(refundOrder.getOrderId())?null:Integer.parseInt(refundOrder.getOrderId()));
		wxPayRecord.setTotalFee((int)(refundOrder.getRefundPrice()*100));
		return wxPayRecord;
	}

	@Override
	public Map request(IWXPay wXPay, PayType payType) throws Exception {
		return wXPay.refund(payType.getReqData(), payType);
	}
	
	/**
	 * 支付请求完成后的执方法
	 */
	@Override
	public void complete(PayType payType){
		RefundOrder refundOrder = (RefundOrder) payType.getObj();
		refundOrder.setAuditTime(new Date());
		if(payType.isSucessed()){
			refundOrder.setStatus(OrderConst.REFUND_ORDER_STATUS_END);
		}else{
			refundOrder.setStatus(OrderConst.REFUND_ORDER_STATUS_ERROR);
		}
		RefundOrderMapper refundOrderMapper = BeanUtils.getBean("refundOrderMapper");
		refundOrderMapper.updateByPrimaryKeySelective(refundOrder);
		return;
	}

	@Override
	public String getOpenId(PayType payType) {
		return ((RefundOrder)payType.getObj()).getOpenid();
	}

	@Override
	public BaseResultVo sendMessage(PayType payType) {
		if(StringUtils.isEmpty(payType.getFormId())){
			return null;
		}
		RefundOrder refundOrder = (RefundOrder)payType.getObj();
    	String page=DictConfig.getKey(DictConst.wx_msg_index_page);
    	String template_id = DictConfig.getKey(DictConst.wx_refund_success_msg_temp);
    	JsonObject jsonObject=new JsonObject();
    	
    	if(payType.isSucessed()){
    		
    		JsonObject key1=new JsonObject();
    		key1.addProperty("value", refundOrder.getOrderNo());
    		jsonObject.add("keyword1", key1);
    		
    		JsonObject key2=new JsonObject();
    		key2.addProperty("value", refundOrder.getRefundNo());
    		jsonObject.add("keyword2", key2);
    		
    		JsonObject key3=new JsonObject();
    		key3.addProperty("value", DateUtils.getSpecifyDate(refundOrder.getApplyTime(),DateUtils.FORMAT_YYYY_MM_DD_HH_MM_SS));
    		jsonObject.add("keyword3", key3);
    		
    		JsonObject key4=new JsonObject();
    		key4.addProperty("value", refundOrder.getRefundPrice().toString());
    		jsonObject.add("keyword4", key4);
    		
    		JsonObject key5=new JsonObject();
    		key5.addProperty("value", "车票退款");
    		jsonObject.add("keyword5", key5);
    	}else{
    		template_id = DictConfig.getKey("wx_refund_faild_msg_temp");
    		
    		JsonObject key1=new JsonObject();
    		key1.addProperty("value", refundOrder.getOrderNo());
    		jsonObject.add("keyword1", key1);
    		
    		JsonObject key2=new JsonObject();
    		key2.addProperty("value", refundOrder.getRefundPrice().toString());
    		jsonObject.add("keyword2", key2);
    		
    		JsonObject key3=new JsonObject();
    		key3.addProperty("value", "车票退款");
    		jsonObject.add("keyword3", key3);
    		
    		JsonObject key4=new JsonObject();
    		key4.addProperty("value", payType.getError());
    		jsonObject.add("keyword4", key4);
    	}
    	
    	TemplateRQ templateRQ=new TemplateRQ();
    	templateRQ.setTouser(payType.getOpenId());
    	templateRQ.setTemplate_id(template_id);
    	templateRQ.setPage(page);
    	templateRQ.setData(jsonObject);
    	templateRQ.setForm_id(payType.getFormId());
    	IWeiXinService iWeiXinService = BeanUtils.getBean("weiXinServiceImpl");
    	String res = iWeiXinService.sendTemplate(templateRQ);
    	return JsonReturnHelper.getSuccessWithData(res);
	}
	

}
